import Vue from "vue";
import Vuex from "vuex";
import getters from "./getters";
import { getColor, setColor, removeColor } from "@/assets/js/auth";

Vue.use(Vuex);

// https://webpack.js.org/guides/dependency-management/#requirecontext
const modulesFiles = require.context("./modules", true, /\.js$/);

// you do not need `import app from './modules/app'`
// it will auto require all vuex module from modules file
const modules = modulesFiles.keys().reduce((modules, modulePath) => {
  const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, "$1");
  const value = modulesFiles(modulePath);
  modules[moduleName] = value.default;
  return modules;
}, {});

const store = new Vuex.Store({
  state: {
    dom: {
      //窗口dom+右侧滚动条
      width: null,
      height: null,
      left: null,
      top: null
    },
    treebox: null, //树形下拉name
    color: getColor() //主题
  },
  mutations: {
    domFn: (state, v) => {
      state.dom = v;
    },
    treeboxFn: (state, v) => {
      state.treebox = v;
    },
    colorFn: (state, v) => {
      state.color = v;
    }
  },
  actions: {
    setColor({ commit }, v) {
      v == "default" ? (v = "") : null;
      !v ? removeColor() : setColor(v);
      commit("colorFn", v);
    }
  },
  modules,
  getters
});

export default store;
